<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
			function cal() {
				var numInput = document.getElementById("num");
				var resultSpan = document.getElementById("result");
				var num = numInput.value;
				
				/*if (num == "") {
					resultSpan.innerHTML = "<span style='color: red;'>不能为空</span>";
					return;
				}*/
				if (isNaN(num)) {
					resultSpan.innerHTML = "<span style='color: red;'>您输入的不是一个数字</span>";
					return;
				}
				if (num < 0) {
					resultSpan.innerHTML = "<span style='color: red;'>负数没有阶乘</span>";
					return;
				} 
				
				if (num == 0) {
					resultSpan.innerHTML = 1;
				} else {
					// n!=n*(n-1)*...*3*2*1
					var result = 1;
					for (var i = num; i >= 1 ; i--) {
						result = result * i;
					}
					resultSpan.innerHTML = result;
				}
			}
		</script>
	</head>
	<body>
		<input type="text" id="num"/>
		<input type="button" value="阶乘" onclick="cal()"/>
		=
		<span id="result"></span>
	</body>
</html>
